package com.example.interfacecall.controller;

import com.alibaba.fastjson.JSON;
import com.example.interfacecall.domain.QuitApplyDto;
import com.example.interfacecall.domain.Result;
import com.example.interfacecall.exception.BusinessException;
import com.example.interfacecall.util.HttpUtils;
import com.example.interfacecall.util.ResultWrapper;
import com.example.interfacecall.util.ResultWrapperMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;

@Validated
@RestController
public class QuitApplyController{
    @Value("${interface.url}")
    private String quitApplyUrl;

    /**
     * 获取退保申请页面
     * @return
     */
    @GetMapping("/quitApplyPage")
    public ModelAndView quitApplyPage() {
        return new ModelAndView("quitapply");
    }

    /**
     * 退保申请
     * @param quitApplyDto
     * @return
     */
    @PostMapping("/quitapply")
    public ResultWrapper<Result> quitapply(@Valid @RequestBody @ModelAttribute QuitApplyDto quitApplyDto) {
        try{
            String interfaceName = "quitapply";
            String url = quitApplyUrl +"/"+ interfaceName;
            String param = JSON.toJSONString(quitApplyDto);
            System.out.println(param);
//            String resultOut = HttpUtils.sendPost(url, param);
//            Result result = JSON.parseObject(resultOut, Result.class);
            Result result = new Result();
            result.setMessage("操作成功");
            result.setCode("1");
            return ResultWrapperMapper.ok(result);
        } catch (BusinessException e){
            return ResultWrapperMapper.wrap(e);
        }
    }
//    /**
//     * 请求前缀(退保申请)
//     **/
//    public static final String REQUEST_PREFIX = "/tbsq";
//
//    @RequestMapping(REQUEST_PREFIX + "/quitApplyPage")
//    public ModelAndView quitApplyPage(){
//        return new ModelAndView("quitapply");
//    }
//
//    @RequestMapping(value = "/quitapply", method = RequestMethod.POST)
//    public ResultWrapper<Result> quitapply(@Valid QuitApplyDto quitApplyDto){
//        try{
//            System.out.println("111111");
//            Result result = new Result();
//            result.setCode("1");
//            result.setMessage("成功啦！");
//            return ResultWrapperMapper.ok(result);
//        } catch (BusinessException e){
//            System.out.println("2222");
//            return ResultWrapperMapper.wrap(e);
//        }
//    }

}
